package com.daichi.controller;


import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.daichi.common.dto.SysMenuDto;
import com.daichi.common.lang.Result;
import com.daichi.entity.SysMenu;
import com.daichi.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Daichi Chou
 * @since 2021-06-05
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {

    @GetMapping("/nav")
    public Result nav(Principal principal) {
        SysUser sysUser = sysUserService.getUserByName(principal.getName());

        // 获取权限信息
        String authority = sysUserService.getUserAuthorityInfo(principal.getName());

        List<String> perms;
        if (StrUtil.isNotEmpty(authority)) {
            perms = Arrays.asList(authority.split(","));
        } else {
            perms = Collections.emptyList();
        }

        // 获取导航栏信息
        List<SysMenuDto> navs = sysMenuService.getUserNav(sysUser);

        return Result.succ(MapUtil.builder().put("authoritys", perms).put("nav", navs).map());

    }

    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result info(@PathVariable("id") Long id) {
        Assert.notNull(id, "id不能为空");
        return Result.succ(sysMenuService.getById(id));
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result list() {
        List<SysMenu> result = sysMenuService.getTreeList();
        return Result.succ(result);
    }

    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:menu:save')")
    public Result save(@Validated @RequestBody SysMenu sysMenu) {
        sysMenuService.clearTreeList();
        sysMenu.setCreated(new Date());
        sysMenuService.save(sysMenu);
        sysMenuService.clearTreeList();
        return Result.succ(sysMenu);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public Result update(@Validated @RequestBody SysMenu sysMenu) {
        sysMenuService.clearTreeList();

        sysMenu.setUpdated(new Date());
        sysMenuService.updateById(sysMenu);

        sysMenuService.clearTreeList();
        sysUserService.clearUserAuthorityInfoByMenuId(sysMenu.getId());

        return Result.succ(sysMenu);
    }

    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result delete(@PathVariable("id") Long id) {
        sysMenuService.clearTreeList();
        sysUserService.clearUserAuthorityInfoByMenuId(id);

        Result result = sysMenuService.deleteById(id);

        sysMenuService.clearTreeList();
        sysUserService.clearUserAuthorityInfoByMenuId(id);

        return result;

    }


}
